<template>
  <div class="login-wrapper">
    <div class="container">
      <form @submit.prevent="handleRegister">
        <div class="card">
          <div class="login-box">
            <h1>注册</h1>

            <div class="form-group">
              <input
                  type="text"
                  id="username"
                  v-model="RegisterDTO.name"
                  placeholder="用户名"
                  required
              >
              <span v-if="errors.name" class="error-message">{{ errors.name }}</span>
            </div>

            <div class="form-group">
              <input
                  type="password"
                  id="password"
                  v-model="RegisterDTO.pwd"
                  placeholder="密码"
                  required
              >
              <span v-if="errors.pwd" class="error-message">{{ errors.pwd }}</span>
            </div>

            <div class="form-group">
              <input
                  type="tel"
                  id="phone"
                  v-model="RegisterDTO.telephone"
                  placeholder="手机号"
                  required
              >
              <span v-if="errors.telephone" class="error-message">{{ errors.telephone }}</span>
            </div>

            <div class="button-container">
              <button type="submit" class="login-btn">注 册</button>
              <router-link to="/login" class="login-btn">返 回</router-link>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { reactive } from "vue";
import { Register } from "@/api";
import { useRouter } from "vue-router";

export default {
  name: "RegisterPage",
  setup() {
    const RegisterDTO = reactive({
      name: '',
      pwd: '',
      telephone: ''
    });

    const errors = reactive({
      name: '',
      pwd: '',
      telephone: ''
    });

    const router = useRouter();

    const validateForm = () => {
      let isValid = true;

      // 用户名验证
      if (!RegisterDTO.name.trim()) {
        errors.name = '用户名不能为空';
        isValid = false;
      } else if (RegisterDTO.name.length < 3 || RegisterDTO.name.length > 20) {
        errors.name = '用户名长度必须在3到20个字符之间';
        isValid = false;
      } else {
        errors.name = '';
      }

      // 密码验证
      if (!RegisterDTO.pwd.trim()) {
        errors.pwd = '密码不能为空';
        isValid = false;
      } else if (RegisterDTO.pwd.length < 6 || RegisterDTO.pwd.length > 20) {
        errors.pwd = '密码长度必须在6到20个字符之间';
        isValid = false;
      } else {
        errors.pwd = '';
      }

      // 手机号验证
      if (!RegisterDTO.telephone.trim()) {
        errors.telephone = '手机号不能为空';
        isValid = false;
      } else if (!/^1[3456789]\d{9}$/.test(RegisterDTO.telephone)) {
        errors.telephone = '手机号格式不正确';
        isValid = false;
      } else {
        errors.telephone = '';
      }

      return isValid;
    };

    const handleRegister = async () => {
      if (!validateForm()) {
        return;
      }

      try {
        const response = await Register(RegisterDTO);
        console.log('Register Response:', response);

        if (response.data.code === 200) {
          alert('注册成功！');
          await router.push('/login');
        } else {
          alert(response.data?.data?.errorMsg || response.data?.errorMsg || '注册失败，请稍后重试');
        }
      } catch (error) {
        console.error('Register API Error:', error);
        alert('请求失败，请稍后再试');
      }
    };

    return {
      RegisterDTO,
      errors,
      handleRegister,
    };
  },
};
</script>

<style scoped>
.login-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/bg2.jpg');
  background-repeat: no-repeat;
  overflow: hidden;
  background-size: 100%;
  background-attachment: fixed;
}

.container {
  text-align: center;
  margin-top: 90px;
  width: 400px;
  height: 500px;
  padding: 60px 0;
  background-color: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  position: relative;
  border-radius: 20px;
}

.card {
  padding: 20px;
}

.login-box h1 {
  font-size: 50px;
  cursor: pointer;
  color: rgba(42, 130, 148);
  margin-bottom: 30px;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

.form-group input {
  width: 250px;
  height: 40px;
  border-radius: 5px;
  margin-bottom: 5px;
  padding-left: 15px;
  outline-style: none;
  border: 1px solid #ccc;
}

.form-group input:hover {
  box-shadow: 2px 2px 5px #555;
  background-color: #ddd;
}

.error-message {
  color: red;
  font-size: 12px;
  position: absolute;
  bottom: -18px;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
}

.button-container {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 30px;
}

.login-btn {
  width: 125px;
  height: 40px;
  background-color: rgba(42, 130, 148);
  cursor: pointer;
  border: none;
  border-radius: 5px;
  color: white;
  font-size: 16px;
  text-decoration: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-btn:hover {
  background-color: rgba(21, 113, 150);
}

@media (max-width: 576px) {
  .container {
    width: 90%;
    padding: 40px 0;
    margin-top: 50px;
  }

  .form-group input {
    width: 90%;
  }

  .button-container {
    flex-direction: column;
    align-items: center;
  }

  .login-btn {
    width: 90%;
    margin-bottom: 10px;
  }

  .error-message {
    left: 5%;
    transform: none;
  }
}
</style>
